#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/6/21 0021 21:09
# @Author  : wzy
# @Site    : 
# @File    : send_email.py
# @Software: PyCharm

import os
import smtplib
from email.utils import formataddr
from Util.operate_config import ope_config
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from Util.operate_pathUrll import pathinfo

on_off = ope_config.get_config_value('on_off','EMAIL')
address = ope_config.get_config_value('addressee','EMAIL')
cc = ope_config.get_config_value('cc','EMAIL')
acceptCode = ope_config.get_config_value('acceptCode','EMAIL')
sub = ope_config.get_config_value('subject','EMAIL')
mail_path = os.path.join(pathinfo.get_project_baseUrl(), 'Result', 'report.HTML')

def send():
    '''
    带附件
    :return:
    '''
    ret = True
    msg = MIMEMultipart()
    msg['From'] = formataddr(["FROM",address])
    msg['To'] = formataddr(["To",address])
    msg['Subject'] = sub
    # xlsx类型附件
    part = MIMEApplication(open(mail_path, 'rb').read())
    part.add_header('Content-Disposition', 'attachment', filename="report.HTML")
    msg.attach(part)
    try:
        server = smtplib.SMTP_SSL("smtp.qq.com", 465)  # 发件人邮箱中的SMTP服务器，端口是25
        server.login(address, acceptCode)  # 括号中对应的是发件人邮箱账号、邮箱密码
        server.sendmail(address,[address,],msg.as_string())  # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
        server.quit()  # 关闭连接
        print("已发送")
        return ret
    except smtplib.SMTPException:
        print("SMTP is NULL")
    return ret
if __name__ == '__main__':
    # print(on_off)
    # print(address)
    # print(cc)
    # print(acceptCode)
    # print(mail_path)
    pass
